home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / NGService / MMText.m < prev    next >
Encoding:
Text File  |  1992-08-10  |  8.4 KB  |  376 lines

  1. /*
  2.  *      You may freely copy, distribute and reuse the code
  3.  *      in this example.  Scott Anguish disclaims any warranty of
  4.  *      any kind, expressed or implied, as to its fitness
  5.  *      for any particular use.
  6.  *        Please keep this notice intact
  7.  *      Written by: Scott Anguish  sanguish@digifix.com
  8.  */
  9.  
  10. #import "MMText.h"
  11.  
  12. #import <libc.h>                      /* MAXPATHLEN */
  13. #import <stdio.h>
  14. #import <strings.h>
  15.  
  16. #import <appkit/appkit.h>
  17.  
  18. #import <streams/streams.h>
  19.  
  20.  
  21. @implementation MMText:Text
  22.  
  23. + initialize
  24. {
  25.     [super initialize];
  26.     [Text registerDirective:"NGFolder" forClass: [MMFolderCell class]];
  27.     [Text registerDirective:"NGGraphic" forClass: [MMGraphicCell class]];
  28.     [Text registerDirective:"NGDocument" forClass: [MMDocumentCell class]];
  29.     return self;
  30. }
  31.  
  32. - (BOOL)isHeaderVisible:(const char*)header
  33. {
  34.     if ((strncmp(header,"From:",5)==0)             && showFrom)  return YES;
  35.     if ((strncmp(header,"Date:",5)==0)             && showDate)  return YES;
  36.     if ((strncmp(header,"Newsgroups:",11)==0)    && showNewsgroups)  return YES;
  37.     if ((strncmp(header,"Subject:",8)==0)        && showSubject)  return YES;
  38.     if ((strncmp(header,"MessageID:",10)==0)    && showMessageID)  return YES;
  39.     if ((strncmp(header,"Path:",5)==0)            && showPath)  return YES;
  40.     if ((strncmp(header,"Followup-To:",12)==0)    && showFollowupTo)  return YES;
  41.     if ((strncmp(header,"Expires:",8)==0)        && showExpires)  return YES;
  42.     if ((strncmp(header,"Reply-To:",9)==0)        && showReplyTo)  return YES;
  43.     if ((strncmp(header,"Sender:",7)==0)        && showSender)  return YES;
  44.     if ((strncmp(header,"Control:",8)==0)        && showControl)  return YES;
  45.     if ((strncmp(header,"Keywords:",9)==0)        && showKeywords)  return YES;
  46.     if ((strncmp(header,"Summary:",8)==0)        && showSummary)  return YES;
  47.     if ((strncmp(header,"Approved:",9)==0)        && showApproved)  return YES;
  48.     if ((strncmp(header,"Lines:",6)==0)            && showLines)  return YES;
  49.     if ((strncmp(header,"Xref:",5)==0)            && showXref)  return YES;
  50.     if ((strncmp(header,"Organization:",13)==0)    && showOrganization)  return YES;
  51.  
  52.     return NO;
  53. }
  54.  
  55. - setVisibleHeaders
  56. {
  57.     showFrom=YES;
  58.     showDate=YES;
  59.     showNewsgroups=NO;
  60.     showSubject=YES;
  61.     showMessageID=NO;
  62.     showPath=NO;
  63.     showFollowupTo=NO;
  64.     showExpires=NO;
  65.     showReplyTo=NO;
  66.     showSender=NO;
  67.     showReferences=NO;
  68.     showControl=NO;
  69.     showDistribution=NO;
  70.     showKeywords=NO;
  71.     showSummary=NO;
  72.     showApproved=NO;
  73.     showLines=NO;
  74.     showXref=NO;
  75.     showOrganization=NO;
  76.  
  77.  
  78.     return self;
  79.  
  80. };
  81. - initFrame:(NXRect *)r
  82. {
  83.     self = [super initFrame:r];
  84.     [self notifyAncestorWhenFrameChanged:YES];
  85.     [self setMonoFont:NO];
  86.     [self setVertResizable:YES];
  87.     [self setHorizResizable:NO];
  88.     [self setSel:0:0];
  89.     [self setOpaque:YES];
  90.  
  91.     { NXSize aSize = {1.0E38,1.0E38};
  92.       [self setMinSize:&r->size];
  93.       [self setMaxSize:&aSize];
  94.   }
  95.     [self setVisibleHeaders];
  96.     return self;
  97. }
  98.  
  99. - free
  100. {    
  101.  
  102.     unsigned int windowNum;
  103.     id speaker = [NXApp appSpeaker];
  104.  
  105.     if (privateListener) 
  106.         {
  107.         [speaker setSendPort: NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
  108.         NXConvertWinNumToGlobal([window windowNum], &windowNum);
  109.         [speaker unregisterWindow:windowNum];
  110.         [privateListener free];
  111.         }
  112.     [super free];
  113.     return self; 
  114. }
  115.  
  116. - (int)iconEntered:(int)windowNum at:(double)x :(double)y
  117.     iconWindow:(int)iconWindowNum iconX:(double)iconX iconY:(double)iconY
  118.     iconWidth:(double)iconWidth iconHeight:(double)iconHeight
  119.     pathList:(char *)pathList
  120. {
  121.  
  122.  
  123.     if (!iconPathList || strcmp(iconPathList, pathList)) 
  124.         {
  125.         if (iconPathList) NX_FREE(iconPathList);
  126.         NX_MALLOC(iconPathList,char, strlen(pathList)+1);
  127.         //iconPathList = NXCopyStringBufferFromZone(pathList, [self zone]);
  128.         strcpy(iconPathList, pathList);
  129.         }
  130.     return 0;
  131. }
  132.  
  133. - (int)iconReleasedAt:(double)x :(double)y ok:(int *)flag
  134. {
  135.     if (![self isEditable]) 
  136.         {
  137.         *flag = NO;
  138.         } else 
  139.         {
  140.         *flag = YES;
  141.         [self perform:@selector(delayIconReleasedAt:) with:self afterDelay:5 cancelPrevious:YES];
  142.         }
  143.     return 0;
  144. }
  145.  
  146. - delayIconReleasedAt:sender
  147. {
  148.     int dummy,*flag;
  149.     
  150.     NXPoint p;
  151.     char *file,  *tab, *extension;
  152.     id    newCell;
  153.     int foundOne ;
  154.  
  155.     flag = &dummy;
  156.     
  157.     foundOne = NO;
  158.  
  159.     [window convertScreenToBase:&p];
  160.     [self convertPoint:&p fromView:nil];
  161.  
  162.  
  163.     
  164.     file = iconPathList;
  165.     while (file) {
  166.         tab = strchr(file, '\t');
  167.         if (tab) *tab = '\0';
  168.         extension = strrchr(file, '.');
  169.         if (extension &&
  170.         ( !strcmp(extension, ".ps")  || !strcmp(extension, ".eps") ||
  171.           !strcmp(extension,".tiff") || !strcmp(extension,".tif")  )) {
  172.             
  173.             newCell =[[MMGraphicCell alloc] imageFromFilename:file];
  174.             [self replaceSelWithCell:newCell];
  175.             foundOne = YES;
  176.             
  177.         } else {
  178.             newCell = [[MMDocumentCell alloc] imageFromFilename:file];
  179.             [self replaceSelWithCell:newCell];
  180.             foundOne = YES;
  181.         }
  182.             
  183.        file = tab ? ++tab : NULL;
  184.     }
  185.     
  186.     *flag = foundOne;
  187.     return 0;
  188. }
  189.  
  190. - fileDragging
  191. {
  192.     unsigned int windowNum;
  193.     id speaker;
  194.  
  195.     speaker = [NXApp appSpeaker];
  196.     if (!privateListener ) {
  197.         privateListener = [Listener new];
  198.         [privateListener setDelegate:self];
  199.         [privateListener usePrivatePort];
  200.         [privateListener addPort];
  201.     }
  202.     if ([self isEditable]) 
  203.         {
  204.         NXConvertWinNumToGlobal([window windowNum], &windowNum);
  205.         [speaker setSendPort: NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
  206.         [speaker registerWindow:windowNum toPort:[privateListener listenPort]];
  207.         } else
  208.         {
  209.         [speaker setSendPort: NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
  210.         NXConvertWinNumToGlobal([window windowNum], &windowNum);
  211.         [speaker unregisterWindow:windowNum];
  212.         };
  213.     return self;
  214. }
  215.  
  216. - becomeFirstResponder
  217. {
  218.     [self fileDragging];
  219.     [super becomeFirstResponder];
  220.     return self;
  221. }
  222.  
  223. - appendBoldWord:(char *)aString
  224. {
  225.     int length;
  226.     char header[BUFSIZ],restofline[BUFSIZ];
  227.  
  228.     sscanf(aString,"%s%[^\n]", header,restofline);
  229.     strcat(restofline,"\n");
  230.     
  231.     if ([self isHeaderVisible:header])
  232.         {
  233.         length = [self textLength];
  234.         [self setSel:length:length];
  235.         [self replaceSel:header];
  236.         [self setSel:(length):[self textLength]];
  237.         [self setSelFont:[Font newFont:"Courier-Bold" size:12.0]];
  238.     
  239.         length = [self textLength];
  240.         [self setSel:length:length];
  241.         [self replaceSel:restofline];
  242.         [self setSel:(length):[self textLength]];
  243.         [self setSelFont:[Font newFont:"Courier" size:12.0]];
  244.     
  245.         length = [self textLength];
  246.         [self setSel:length:length];
  247.         };
  248.     return self;
  249.  
  250. };
  251.  
  252. - appendRichFile:(char *)aFile
  253. {
  254.     NXStream *aStream;
  255.     int length;
  256.  
  257.     length = [self textLength];
  258.     [self setSel:length:length];
  259.     aStream=NXMapFile(aFile,NX_READONLY);
  260.     [self replaceSelWithRichText:aStream];
  261.     NXClose(aStream);
  262.     return self;
  263. };
  264.  
  265. - openFileName:(char *)aFile
  266. {
  267.     [self openFileName:aFile ignoreHeader:NO];
  268.     return self;
  269. }
  270.  
  271. - openFileName:(char *)aFile ignoreHeader:(BOOL)ignoreHeader
  272. {
  273.  
  274.     FILE    *filePtr,*outfile;
  275.     char     theLine[BUFSIZ];
  276.     BOOL     NGfound;
  277.     
  278.     [self setText:""];
  279.     NGfound=NO;
  280.     
  281.     [[self window] disableFlushWindow];
  282.     [self setEditable:NO];
  283.     filePtr = fopen(aFile, "r");
  284.     if (ignoreHeader)
  285.         {
  286.         while (fgets(theLine,BUFSIZ, filePtr) != NULL)
  287.             {
  288.                 if (strncmp(theLine, "\n",1)==0)
  289.                     {
  290.                         break;
  291.                     };
  292.                 [self appendBoldWord:theLine];
  293.             };
  294.  
  295.         [self replaceSel:"\n\n"];
  296.         }
  297.     while (fgets(theLine,BUFSIZ, filePtr) != NULL)
  298.         {
  299.             
  300.             if (strncmp(theLine, NGHEADER,strlen(NGHEADER))==0)
  301.                 {
  302.                     NGfound=YES;
  303.                     break;
  304.                 };
  305.         };
  306.     if (NGfound)
  307.         {
  308.  
  309.         if (outfile = popen("rm /tmp/NGtemp; /usr/bin/uudecode ","w"))
  310.             {
  311.             fprintf(outfile,"begin 644 /tmp/NGtemp\n");
  312.             while (fgets(theLine,BUFSIZ, filePtr) != NULL) 
  313.                 {if (strncmp(theLine,"`",1)==0) 
  314.                     {
  315.                         fprintf(outfile,"`\n");
  316.                         break;
  317.                     }
  318.                     else
  319.                     fputs(theLine,outfile);
  320.                 };
  321.             fprintf(outfile,"end\n");
  322.             if (pclose(outfile)==0) [self appendRichFile:"/tmp/NGtemp"];
  323.                 else NXRunAlertPanel("NGService","Unable to decode! Check selection.",
  324.            NULL, NULL, NULL);
  325.             (void)remove("/tmp/NGtemp");
  326.             };
  327.  
  328.         };
  329.     if (!NGfound) NXRunAlertPanel("NGService","I could not find any NewsGrazer Data!",
  330.            NULL, NULL, NULL);
  331.     fclose(filePtr);
  332.     [self sizeToFit];
  333.     [[[self window] reenableFlushWindow] flushWindow];
  334.     [self display];
  335.     return self;
  336.  
  337. }
  338.  
  339. - readFromMemory:(char *)data length:(int)theSize
  340. {
  341.  
  342.     NXStream *aStream;
  343.     char filename[MAXPATHLEN+1];
  344.     
  345.     [self setText:""];
  346.     
  347.     strcpy(filename,"/tmp/file000000Selection" );
  348.     NXGetTempFilename(filename,9);
  349.     aStream = NXOpenMemory(data, theSize ,NX_READONLY);
  350.     NXSaveToFile(aStream, filename);
  351.     NXCloseMemory(aStream,NX_FREEBUFFER);
  352.     [self openFileName:filename];
  353.     (void)remove(filename);
  354.     return self;
  355.  
  356. }
  357.  
  358.  
  359. - saveFileName:(char *)aFile
  360. {
  361.     NXStream *aStream;
  362.  
  363.     aStream = NXOpenMemory(NULL,0, NX_WRITEONLY);
  364.     
  365.     [self writeRichText:aStream];
  366.     
  367.     NXSaveToFile(aStream, aFile);
  368.     NXCloseMemory(aStream, NX_FREEBUFFER);
  369.     
  370.     return self;
  371. }
  372.  
  373.  
  374.  
  375. @end
  376.